package per.qiao.simplerest.scanner;

import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import per.qiao.simplerest.restservice.RestService;

import java.util.Set;

/**
 * Company: 上海数惠系统技术有限公司
 * Department: 数据中心
 * Date: 2020-05-29 13:25
 *
 * @Author: qyp
 * Email: qiaoyp@dist.com.cn
 * Desc: 默认的RestService接口扫描器
 */
public class RestServiceScanner extends AbstractRestServiceScanner {

    @Override
    public boolean supports(Class clazz) {
        if (!ObjectUtils.isEmpty(clazz.getInterfaces()) && matched(clazz)) {
            return true;
        }
        return false;
    }

    protected boolean matched(Class pkgClass) {

        if (pkgClass == null) {
            return false;
        }

        if (RestService.class.isAssignableFrom(pkgClass)) {
            return true;
        }

        Class[] interfaces = pkgClass.getInterfaces();
        if (!ObjectUtils.isEmpty(interfaces)) {
            for (Class aClass : interfaces) {
                matched(aClass);
            }
        }
        Class superclass = pkgClass.getSuperclass();
        return matched(superclass);
    }

    @Override
    protected Set<Class> innerScanner(Set<Class> allMetcher) {

        return allMetcher;
    }
}
